package com.example.elemboot.Controller;

import com.example.elemboot.Entity.User;
import com.example.elemboot.Service.IUserService;
import com.example.elemboot.Utils.AuthAccess;
import com.example.elemboot.Utils.Result;
import com.example.elemboot.Utils.TokenUtils;
import com.example.elemboot.Vo.UserRegitserVo;
import com.example.elemboot.Vo.UserVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping(value = "/login")
    public Result login(@RequestParam("phone") String phone, @RequestParam("pwd") String pwd){
        UserVO login = userService.login(phone,pwd);
        if(login == null){
            return Result.error("手机号或者密码错误");
        }
        return Result.success(login);
    }

    @PostMapping(value = "/register")
    public Result register(@RequestBody UserRegitserVo userRegitserVo){
        User user = new User();
        BeanUtils.copyProperties(userRegitserVo,user);
        user.setUserId(null);
        if (!userService.register(user)){
            return Result.error("手机号已被注册");
        }
        return Result.success("注册成功");
    }

}
